import pygame

from constants.Constants import *


class Role(pygame.sprite.Sprite):

    def __init__(self, screen, image, direction=UP, top=0, left=0, live=True, stop=False, speed=10):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        self.image = image
        self.direction = direction
        self.width = self.image.get_size()[0]
        self.height = self.image.get_size()[1]
        self.rect = self.image.get_rect()
        self.rect.top = top
        self.rect.left = left
        self.live = live
        self.stop = stop
        self.speed = speed
        self.last_top = self.rect.top
        self.last_left = self.rect.left

    def draw(self):
        self.screen.blit(self.image, self.rect)

    def reset_play(self):
        self.rect.top = self.last_top
        self.rect.left = self.last_left
        self.draw()

    def play(self):
        self.draw()

        if self.stop or not self.live:
            return None

        self.last_top = self.rect.top
        self.last_left = self.rect.left

        if self.direction == UP:
            self.rect.top -= self.speed
            if self.rect.top < 0:
                self.rect.top = 0

        elif self.direction == DOWN:
            self.rect.top += self.speed
            if self.rect.top > GAME_WINDOW_HEIGHT - self.height:
                self.rect.top = GAME_WINDOW_HEIGHT - self.height

        elif self.direction == LEFT:
            self.rect.left -= self.speed
            if self.rect.left < 0:
                self.rect.left = 0

        elif self.direction == RIGHT:
            self.rect.left += self.speed
            if self.rect.left > GAME_WINDOW_WIDTH - self.width:
                self.rect.left = GAME_WINDOW_WIDTH - self.width